package com.example.newnoke;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.example.newnoke.data.UserInfo;
import com.example.newnoke.ui.LoginActivity;
import com.example.newnoke.utils.GsonUtil;
import com.example.newnoke.utils.PrefUtils;
import com.geya.jbase.constant.RequestType;
import com.geya.jbase.uiview.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;

public class APP extends Application {

    private static Context mContext;


    private static UserInfo mUesrInfo;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        keepLogin();

        OkGo.getInstance().init(this);
        if (mUesrInfo != null) {
            HttpParams params = new HttpParams();
            params.put("mid", mUesrInfo.getId());
            params.put("token",mUesrInfo.getToken());


            OkGo.getInstance().
                    addCommonParams(params)
            ;
        }
        RequestType.getInstance()
                .init(this)
                .setPageIndex("start")//页码
                .setPageSize("limit")//条目数
                .setIsString(false)//后台返回码 是否是字符串
                .setIsSuccess("code")//后台返回状态码key
                .setMessage("message")//后台返回消息字段key
                .setListDatas("data")//后台返回数据字段key
                .setAgainLogin("-3")//特定错误码（重新登陆等 可以注册特定广播处理）
                .setMainColor(R.color.main)//app主色调
                .setTopBarColor(0xFF00a2ff) //头部颜色
                .setTitleColor(0xFFFFFFFF)//标题颜色
                .setSubheadingColor(0xFFFFFFFF)//副标题颜色
                .setImgDrawable(R.mipmap.img_wu)//无数据图片
                .setImgBack(R.mipmap.icon_fanhui)//返回按钮图片
                .setAddress("http://www.luokelian.com")
                .setStringCodes("200")
                .setIntCodes(200)
        ;
    }

    public static void saveUserInfo(String info) {
        PrefUtils.setString(mContext, "info", info);
        if (info.equals("")) {
            mUesrInfo = null;
            if (mUesrInfo != null) {
//                HttpParams params = new HttpParams();
//                OkGo.getInstance().
//                        getCommonParams().clear();

            }
        } else {
            mUesrInfo = GsonUtil.GsonToBean(info, UserInfo.class);
            if (mUesrInfo != null) {
                HttpParams params = new HttpParams();
                params.put("mid", mUesrInfo.getId());
                params.put("token", mUesrInfo.getToken());

                OkGo.getInstance().
                        addCommonParams(params)
                ;
            }
        }

    }

    public static void keepLogin() {
        String json = PrefUtils.getString(mContext, "info", "");
        if (!json.equals("")) {
            mUesrInfo = GsonUtil.GsonToBean(json, UserInfo.class);

        } else {
            mUesrInfo = null;
        }
    }

    public static UserInfo getUesrInfo() {
        return mUesrInfo;
    }

    public static Context getmContext() {
        return mContext;
    }


    public static void startLogin(){
        APP.saveUserInfo("");
        Intent intent = new Intent(mContext,LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
        ToastUtil.showShort("身份验证失败 请重新登陆");
    }

}
